const webpack = require('webpack'); //to access webpack runtime
const configuration = require('./webpack.build.module.conf');
const {fetchEntries} = require('./util');

const path = require('path');
const modules = fetchEntries(path.join(__dirname,'../src/modules'));

const moduleId = process.argv[2];

let compiler;

Object.keys(modules).forEach(n=>{
  if(moduleId!=null){
    if(n!=moduleId)return;
  }else{
    if(['login','desk','test'].find(m=>m==n)!=null)return;
  }
  console.info('开始打包模块:',n);
  compiler = webpack(configuration(n,true));
  new webpack.ProgressPlugin().apply(compiler);
  compiler.run(function(err, stats) {
    if(stats.hasErrors()){
      stats.compilation.errors.forEach(n=>{
        console.error(n.message);
      })
      throw new Error("编辑失败");
    }else{
      console.info('\n模块编辑成功:',n);
    }
  });
})